package org.game.porker.porker24.servlet;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

/**
 * Application Lifecycle Listener implementation class UnregisterDbListener
 *
 */
public class UnregisterDbListener implements ServletContextListener {
	private static Logger logger = Logger.getLogger(UnregisterDbListener.class);
	/**
	 * Default constructor.
	 */
	public UnregisterDbListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		// derby 驱动老是驻留，显式把它干掉
		logger.debug("try to destroy db driver...");
		/*
		 * 获取所有驱动
		 */
		Enumeration<Driver> drivers = DriverManager.getDrivers();
		while (drivers.hasMoreElements()) {
			Driver driver = drivers.nextElement();
			try {
				//删除驱动
				DriverManager.deregisterDriver(driver);
				logger.info(driver.getClass().getName());
			} catch (SQLException e) {
				logger.error("Error deregistering driver " + driver.getClass().getName(),
						e);
			}
		}
		//问题还是没有解决。先放一放,只能每次都重启tomcat了
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
	}

}
